<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('Admin_model');
	}
	 
	 /*显示登录页面*/
	public function index()
	{
		$this->load->view('login');
	}

	public function check()
	{
	    /*设置表单的验证规则*/
		$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
        $this->form_validation->set_rules('password', 'Password', 'required');

		/*如果客户端验证失败则重新加载视图*/
	   if ($this->form_validation->run() == FALSE){
			$this->load->view('login');
		}
		/*如果客户端验证成功则将表单内容处理后交给模型*/
		else{
	     $username=$this->input->post('username');
		 $password=md5($this->input->post('password'));
		 /*从数据库中查询用户信息表返回查询结果的数组*/
		 $query=$this->Admin_model->login_user($username,$password);
		
        	/*如果查询结果不为空设置用户的session信息并跳转页面*/		
			if($query){
				$session=array('uid'=>$query['uid'],'username'=>$query['username'],'usertype'=>$query['usertype']);
				$this->session->set_userdata($session);
				redirect('home/index');
			}
			/*如果服务器端验证失败返回错误信息*/
			else{
				echo "用户或密码错误";
			}
		}
	}
	/*退出登录，销毁session，跳转至登录页面*/
	public function login_out()
	{
		$this->session->sess_destroy();
		redirect('login/index');
	}
	
	
}
